﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using DAO;
using BUS;

namespace ProjectM2Work.Pages
{
    public partial class PageDichCau : PhoneApplicationPage
    {
        public PageDichCau()
        {
            InitializeComponent();
            // PhanTichCau("học sinh    học  sinh học");
            meVideo.MediaEnded += new RoutedEventHandler(myMediaElement_MediaEnded);
        }

        private void myMediaElement_MediaEnded(object sender, RoutedEventArgs e)
        {
            if (indexHienTai < Max - 1)
            {
                indexHienTai++;
                PlayVideo(indexHienTai);

            }
            else
            {
                meVideo.Stop();
                indexHienTai = 0;
            }
        }

        private int indexHienTai = -1;
        private int Max = 0;
        private List<TuDien> LstResuft = null;
        private List<TextBlock> lstTextBlock = null;
        private Point Pos = new Point(150, 150);

        private void imgDich_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {



        }

        private void imgDich_MouseLeave(object sender, MouseEventArgs e)
        {
            App.ZoomInImage(sender);
        }

        private void imgDich_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            App.ZoomOutImage(sender);
        }

        private void imgDich_MouseMove(object sender, MouseEventArgs e)
        {
            App.ZoomOutImage(sender);
        }

        private void imgDich_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (tbDienTu.Text == "")
            {
                MessageBox.Show("Vui Lòng Nhập Từ Trước Khi Dịch");
                return;
            }
            TuVungBUS tuVungBus = new TuVungBUS();
            //Lấy Các từ trong câu dịch dc.
            LstResuft = tuVungBus.PhanTichCau(tbDienTu.Text);
            Max = LstResuft.Count;
            if (Max == 0)
                return;
            //Margin="132,140,83,252"
            lstTextBlock = new List<TextBlock>();

            Panel.Children.Clear();
            for (int i = 0; i < LstResuft.Count; i++)
            {
                TextBlock temp = new TextBlock();
                temp.Name = (i + 1).ToString("00");
                temp.Text = LstResuft[i].VnWord;
                temp.Margin = new Thickness(8, 4, 0, 0);
                //if (i == 0)
                //    temp.Margin = new Thickness(Pos.X, Pos.Y, 0, 0);
                //else
                //    temp.Margin = new Thickness(Pos.X + lstTextBlock[i - 1].Margin.Right, Pos.Y, 0, 0);
                lstTextBlock.Add(temp);
                Panel.Children.Add(temp);
            }
            Panel.UpdateLayout();
            indexHienTai = 0;
            PlayVideo(indexHienTai);
        }

        private void image1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

            if (indexHienTai > 0)
            {
                indexHienTai--;
                PlayVideo(indexHienTai);
            }
        }

        private void image2_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (indexHienTai <= Max - 1)
            {
                indexHienTai++;
                PlayVideo(indexHienTai);
            }

        }

        public void PlayVideo(int index)
        {
            if (LstResuft != null)
            {
                if (index >= 0 && index < LstResuft.Count)
                {
                    meVideo.Source = PublicAndStatic.ConvertToUri(LstResuft[index]);
                    meVideo.Play();
                }

                HighLightTextBlock();
                //Xử lý Image Back và Next
                if (index == 0)
                {
                    BitmapImage tn = new BitmapImage();
                    tn.SetSource(
                        Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_video_back01.png",
                                                              UriKind.Relative)).Stream);
                    imgBack.Source = tn;

                    tn = new BitmapImage();
                    tn.SetSource(
                        Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_video_next02.png",
                                                              UriKind.Relative)).Stream);
                    imgNext.Source = tn;
                }
                if (index == Max - 1)
                {
                    BitmapImage tn = new BitmapImage();
                    tn.SetSource(
                        Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_video_back02.png",
                                                              UriKind.Relative)).Stream);
                    imgBack.Source = tn;

                    tn = new BitmapImage();
                    tn.SetSource(
                        Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_video_next01.png",
                                                              UriKind.Relative)).Stream);
                    imgNext.Source = tn;
                }
                if (index > 0 && index <= Max - 1)
                {
                    BitmapImage tn = new BitmapImage();
                    tn.SetSource(
                        Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_video_back01.png",
                                                              UriKind.Relative)).Stream);
                    imgBack.Source = tn;

                    tn = new BitmapImage();
                    tn.SetSource(
                        Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_video_next01.png",
                                                              UriKind.Relative)).Stream);
                    imgNext.Source = tn;
                }
            }
        }

        public void HighLightTextBlock()
        {
            for (int i = 0; i < lstTextBlock.Count; i++)
            {
                lstTextBlock[i].Foreground = brushFirst;
                lstTextBlock[i].FontSize = 20;
                if (i == indexHienTai)
                {
                    lstTextBlock[i].FontSize = 30;
                    lstTextBlock[i].Foreground = brushChange;
                }

            }
        }

        private void tbDienTu_LostFocus(object sender, RoutedEventArgs e)
        {

        }

        private void tbDienTu_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
        SolidColorBrush brushFirst = new SolidColorBrush(Colors.Gray);
        SolidColorBrush brushChange = new SolidColorBrush(Colors.Red);
        SolidColorBrush brushText = new SolidColorBrush(Colors.Black);
        string strTimKiem = "Nhập Từ Vào Đây";
        private void tbDienTu_GotFocus(object sender, RoutedEventArgs e)
        {
            if (tbDienTu.Text == strTimKiem)
            {
                tbDienTu.Text = "";
                tbDienTu.Foreground = brushText;
            }
            else
            {
                tbDienTu.Select(0, tbDienTu.Text.Length);
            }
        }

    }
}